package com.css.zfzx.fxpg.modules.economicloss.compute.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.zfzx.fxpg.common.utils.ServerUtil;
import com.css.zfzx.fxpg.modules.economicloss.compute.service.FxEconomyLossRiskEvaluateService;
import com.css.zfzx.fxpg.modules.economicloss.middle.viewobjects.FxEconomyLossMiddleQueryParams;
import com.css.zfzx.fxpg.modules.economicloss.probability.viewobjects.FxEconomyLossProbabilityComputeQueryParams;
import com.css.zfzx.fxpg.modules.middle.viewobjects.FxMiddleResultQueryParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author andy
 * @date 2020-11-11
 */
@Slf4j
@RestController
@RequestMapping("/fxEconomyLossRiskEvaluate")
public class FxEconomyLossRiskEvaluateController {

    @Autowired
    private FxEconomyLossRiskEvaluateService fxBuildingResistanceAbilityEvaluateService;

    @PostMapping("/economicLossEstimateCompute")
    public RestResponse computeEconomicLossEstimateValue(HttpServletRequest request, FxEconomyLossMiddleQueryParams queryParams) {
        RestResponse response = null;
        try{
            String userId = ServerUtil.getUserId(request);
            response = RestResponse.succeed(fxBuildingResistanceAbilityEvaluateService.computeEconomicLossEstimateValue(queryParams, userId));
        }catch (Exception e){
            String errorMessage = "计算失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping("/districtEconomicLossCertaintyLoss")
    public RestResponse districtEconomicLossCertaintyLoss(HttpServletRequest request, FxEconomyLossMiddleQueryParams queryParams) {
        RestResponse response = null;
        try{
            String userId = ServerUtil.getUserId(request);
            response = RestResponse.succeed(fxBuildingResistanceAbilityEvaluateService.districtEconomicLossCertaintyLoss(queryParams, userId));
        }catch (Exception e){
            String errorMessage = "区划失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping("/districtEconomicLossProbabilityLoss")
    public RestResponse districtEconomicLossProbabilityLoss(HttpServletRequest request, FxEconomyLossMiddleQueryParams queryParams) {
        RestResponse response = null;
        try{
            String userId = ServerUtil.getUserId(request);
            response = RestResponse.succeed(fxBuildingResistanceAbilityEvaluateService.districtEconomicLossProbabilityLoss(queryParams, userId));
        }catch (Exception e){
            String errorMessage = "区划失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping("/computeEconomicLossProbability")
    public RestResponse computeEconomicLossProbability(HttpServletRequest request, FxEconomyLossProbabilityComputeQueryParams queryParams) {
        RestResponse response = null;
        try{
            String userId = ServerUtil.getUserId(request);
            response = RestResponse.succeed(fxBuildingResistanceAbilityEvaluateService.computeEconomicLossProbability(queryParams, userId));
        }catch (Exception e){
            String errorMessage = "计算出现错误!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(fxBuildingResistanceAbilityEvaluateService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

}